import numpy as np
import torch
from torch.utils.data import Dataset,DataLoader


class DiabetesDataset(Dataset):
    def __init__(self):
        xy = np.loadtxt('diabetes.csv.gz', delimiter=',', dtype=np.float32)
        self.len = xy.shape[0]
        self.x_data = torch.from_numpy(xy[:, :-1])
        self.y_data = torch.from_numpy(xy[:, [-1]])

    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    def __len__(self):
        return self.len


dataset = DiabetesDataset()

train_loader = DataLoader(dataset=dataset,
                          batch_size=32,
                          shuffle=True,
                          num_workers=2)

class Model(torch.nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.liner1 = torch.nn.Linear(8,6)
        self.liner2 = torch.nn.Linear(6,4)
        self.liner3 = torch.nn.Linear(4,1)
        self.sigmoid = torch.nn.Sigmoid()

    def forward(self,x):
        x = self.sigmoid(self.liner1(x))
        x = self.sigmoid(self.liner2(x))
        x = self.sigmoid(self.liner3(x))
        return x

model = Model()

criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(),lr=0.01)

if __name__ == '__main__':
    for epoch in range(100):
        for i,data in enumerate(train_loader,0):
            #1.Prepare data
            inputs,labels = data
            #2.Forward
            y_pred = model(inputs)
            loss = criterion(y_pred,labels)
            print(epoch,i,loss.item())
            #3.Backword
            optimizer.zero_grad()
            loss.backward()
            #4.Update
            optimizer.step()
